<template >
  <div class="innerPageTable-box-OneTwo value-set-list-wrap innerPageTable-out-flex">
    <!-- 搜索区域 -->
    <SelfAdaption>
      <Form
        :model="searchForm"
        ref="serviceForm"
        slot="adaption-bar"
        label-position="top"
        class="ivu-form-no-margin-bottom"
        inline
      >
        <Form-item prop="vsCode" :label="'规则名称：'">
          <Input type="text" v-model.trim="searchForm.ruleName" :placeholder="'请输入'"></Input>
        </Form-item>

        <Form-item prop="vsCode" :label="'报表名称：'">
          <Input type="text" v-model.trim="searchForm.reportName" :placeholder="'请输入'"></Input>
        </Form-item>

        <Form-item prop="vsCode" :label="'状态：'">
          <Select
            style="width: 100%"
            clearable
            placeholder="请选择"
            v-model="searchForm.state"
          >
            <Option v-for="item in stateList" :value="item.value" :key="item.value">{{ item.text }}</Option>
          </Select>
        </Form-item>
        <!--  查询 -->
        <Form-item prop="button" class="search-solt-btn">
          <RPButton optType="search" ref='search' @click="searchRuleConfig" />
        </Form-item>
      </Form>
    </SelfAdaption>
    <InnerPageTable ref="RuleConfigTable" :config="pageTableConfig" v-if='showTable'>
      <div slot="table-bar">
        <RPButton optType="create" type="info" @click="createConfig" />
      </div>
    </InnerPageTable>
  </div>
</template>

<script>
import { ruleConfigColumns, columnListRuleConfig } from './columns'
import ruleConfigApi from '../../../api/ruleConfig.js'
import createConfig from './ruleConfigComponents/createConfig.vue'
import tableColumnSetting from '@/mixin/tableColumnSetting'
import sideDialog from '@/components/sideDialog'
import coverApi from '@/api/cover.js'
import {
  RULE_CONFIG_STATE_LIST,
  RULE_CONFIG_STATE_ON,
} from './const.js'
export default {
  name: 'processed',
  mixins: [tableColumnSetting],
  data() {
    return {
      bussinessUnit: [],
      searchForm: {
        ruleName: '',
        reportName: ''
      },
      pageTableConfig: {
        loadDataFn: this.getPageInfoList.bind(this),
        columns: ruleConfigColumns.bind(this)(),
        autoFirst: false //默认 就是 false, 要配置在这里
      },
      stateList: RULE_CONFIG_STATE_LIST,
      columnList: columnListRuleConfig,
      menuNo: 'ruleConfig',
      iframeLoginUrl: '',
      iframeSrc: ''
    }
  },
  created() {},
  beforeMount: function () {},
  beforeDestroy: function () {},
  destroyed: function () {},
  methods: {
    init() {
      this.searchRuleConfig()
      this.getIframeLoginUrl()
    },
    getIframeLoginUrl() {
      coverApi.getCodeList('IFRAME_OUT_URL').then((res) => {
        let rescords = res.data.records
        if (rescords.length > 0) {
          let list = rescords.filter(item => 'SIGNON_LOGIN_URL' == item.vsiKey) || []
          if (list.length == 1) {
            this.iframeLoginUrl = list[0].vsiDescription
          }
        }
      })
    },
    // 保留勿删，低代码引入开发模式
    createConfig2() {
      coverApi.getCodeList('IFRAME_OUT_URL').then((res) => {
        let rescords = res.data.records
        if (rescords.length > 0) {
          let list = rescords.filter(item => 'RULE_CONFIG_CREATE_URL' == item.vsiKey) || []
          if (list.length == 1) {
            this.iframeSrc = list[0].vsiDescription
            new sideDialog(
              this.iframeSrc,
              {
                fromApp: 'signon',
                iframeLoginUrl: this.iframeLoginUrl
              },
              data => {
                data && this.searchRuleConfig()
              }
            )
          }
        }
      })
    },
    createConfig() {
      new this.$sidelipPage(
        createConfig,
        {
          props: {
            title: '新建提醒规则',
            action: 'create',
            disabledPage: false,
          }
        },
        data => {
          data && this.searchRuleConfig()
        }
      )
    },
    goDetail(row) {
      new this.$sidelipPage(
        createConfig,
        {
          props: {
            title: '提醒规则详情',
            id: row.id,
            action: 'detail',
            disabledPage: true,
          }
        },
        data => {
          data && this.searchRuleConfig()
        }
      )
    },
    // 保留勿删，低代码引入开发模式
    goDetail2(row) {
      coverApi.getCodeList('IFRAME_OUT_URL').then((res) => {
        let rescords = res.data.records
        if (rescords.length > 0) {
          let list = rescords.filter(item => 'RULE_CONFIG_DETAIL_URL' == item.vsiKey) || []
          if (list.length == 1) {
            this.iframeSrc = list[0].vsiDescription
            new sideDialog(
              this.iframeSrc,
              {
                fromApp: 'signon',
                iframeLoginUrl: this.iframeLoginUrl
              },
              data => {
                data && this.searchRuleConfig()
              }
            )
          }
        }
      })
    },
    goEdit(row) {
      new this.$sidelipPage(
        createConfig,
        {
          props: {
            disabledPage: false,
            id: row.id,
            action: 'edit',
            title: '编辑提醒规则'
          }
        },
        data => {
          data && this.searchRuleConfig()
        }
      )
    },
    // 保留勿删，低代码引入开发模式
    goEdit2(row) {
      coverApi.getCodeList('IFRAME_OUT_URL').then((res) => {
        let rescords = res.data.records
        if (rescords.length > 0) {
          let list = rescords.filter(item => 'RULE_CONFIG_EDIT_URL' == item.vsiKey) || []
          if (list.length == 1) {
            this.iframeSrc = list[0].vsiDescription
            new sideDialog(
              this.iframeSrc,
              {
                fromApp: 'signon',
                iframeLoginUrl: this.iframeLoginUrl
              },
              data => {
                data && this.searchRuleConfig()
              }
            )
          }
        }
      })
    },
    startOrStopRule(row) {
      let state = row.state
      let title = state == RULE_CONFIG_STATE_ON ? '关闭规则' : '开启规则'
      let content = state == RULE_CONFIG_STATE_ON ? `确认关闭${row.ruleName}？` : `确认开启${row.ruleName}？`
      this.$confirm({
        title,
        content
      }).then(()=>{
        ruleConfigApi.startOrStopRule({
          id: row.id
        }).then((res) => {
          if (res.data.code == 200) {
            this.$Message.success({
              content: '操作成功',
              duration: 3
            })
            this.searchRuleConfig()
          } else {
            this.$Message.error({
                content: res.data.msg || '操作失败',
                duration: 3
            });
          }
        })
      })
    },
    goDelete(row) {
      this.$confirm({
        title:"规则删除",
        content: `确认删除 【${row.ruleName}】？`
      }).then(()=>{
        ruleConfigApi.deleteInfo({
          data: [row.id]
        }).then(() => {
          this.$Message.success({
            content: '删除成功',
            duration: 3
          })
          this.searchRuleConfig()
        })
      })
    },
    getPageInfoList: tableObj => {
      let { responseProcess, param } = tableObj
      let { pageNum, pageSize, ruleName, reportName, state } = param
      ruleConfigApi.getTableList({
        reportName,
        ruleName,
        state,
        currentPage: pageNum,
        pageSize,
        sortOrderBy: "DESC",
        sortPropName: "create_time"
      }).then(res => {
       let { current, size, records, pages, total  } = res.data.data
        responseProcess({
          pageNum: current,
          size,
          pages,
          records: records || [],
          total,
        });
      })
    },
    //搜索
    searchRuleConfig() {
      this.$refs['RuleConfigTable'].load(this.searchForm)
    },
  },
  directives: {}
}
</script>
<style lang="less" scoped>
/deep/.table-bar button:last-child {
  color: #ffffff;
  background-color: #ff6f4b;
  border-color: #ff6f4b;
}
</style>
